<template>
  <el-dialog
    :title="dTitle"
    :visible.sync="visible"
    width="50%"
    :before-close="closeDialog"
  >
    <el-form
      :model="formInline"
      class="form-style"
      :inline="true"
      label-width="100px"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="当前团长：">
            <template>
              <p>{{ UserName }}</p>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="上级团长：">
            <template>
              <p v-if="this.leadersName">{{ leadersName }}</p>
              <p else>--</p>
            </template>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item>
            <el-input
              placeholder="团长姓名"
              v-model="formInline.name"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item>
            <el-button @click="search">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-style">
      <el-table :data="tableData" border>
        <el-table-column prop="SalesManName" label="团长姓名"></el-table-column>
        <el-table-column prop="SalesManPhone" label="手机号"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="IsInvite(scope.row.UserId, scope.row.SalesManName)"
              >设为上级</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <botpage
        class="botpage-style"
        :currentPage="currentPage"
        :PageSize="PageSize"
        :total="total"
        @handleCurrentChange="currentChange"
        @handleSizeChange="pageSizeChange"
      />
    </div>
  </el-dialog>
</template>

<script>
import { LoadSalesMan, UpdateSalesManIsInvite } from "@/request/api";
import Botpage from "@/components/botpage/Botpage";
export default {
  props: {
    dTitle: {
      type: String,
      default: "",
    },
    visible: {
      type: Boolean,
      default: false,
    },
    // classifyId: {
    //   type: Array,
    //   default: []
    // },
    UserId: {
      type: String,
      default: "",
    },
    UserName: {
      type: String,
      default: "",
    },
  },
  watch:{
    visible(){
      if(this.visible){
        this.getTableInfo();
      }
    }
  },
  methods: {
    //设为上限
    IsInvite(row, fatherusername) {
      console.log(
        "当前团长name："+ this.UserName,
        "团长id：" + this.UserId,
        "想选择的团长id：" + row,
        "想选择的团长name：" + fatherusername
      );
      UpdateSalesManIsInvite({
        UserId: this.UserId,
        FatherUserId: row,
      })
        .then((res) => {
          if (res.StatusCode === 200) {
            this.$confirm("调整团长操作成功！", "调整成功", {});
          }
        })
        .catch((err) => { console.log(err)});
    },
    //表格信息
    getTableInfo() {
      LoadSalesMan({
        OtherValues: this.UserId,
        PageIndex: this.currentPage,
        PageSize: this.PageSize,
        OS: "PC",
        CustomerId: 1,
        SreachValues: this.formInline.name,
      })
        .then((res) => {
          if (res.StatusCode === 200) {
            this.tableData = res.Data;
            for (let i in this.tableData) {
              if (this.UserId === this.tableData[i].UserId) {
                console.log(i)
                this.tableData.splice(i, 1);
                break;
              }
              // this.username = this.tableData[i].InviteBySalesManName;
              // console.log(this.username)
              this.leadersName =  this.tableData[i].InviteBySalesManName;
              console.log('页面传递数据----->>'+this.UserId,'后台返回数据===>>>>'+this.tableData[i].UserId, "后台返回名称=====>>>"+ this.leadersName);
            }
            this.currentPage = res.PageIndexs;
            this.PageSize = res.PageCount;
            this.total = res.DataCounts;

          }
          // console.log(this.tableData);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //搜索
    search() {
      this.getTableInfo();
    },
    closeDialog(reload) {
      this.$emit("closeDialog", reload);
    },
    currentChange(event) {
      this.currentPage = event;
      this.getTableInfo();
    },
    pageSizeChange(event) {
      this.PageSize = event;
      this.getTableInfo();
    },
  },
  data() {
    return {
      leadersName:"",
      username: "",
      currentPage: 1,
      PageSize: 10,
      total: 1,
      formInline: {},
      // classifyId: [],
      tableData: [],
    };
  },
  mounted() {
    // this.getTableInfo();
    // console.log(this.UserId)
  },
  components: {
    Botpage,
  },
};
</script>

<style lang="less" scoped>
</style>